from playsound import playsound
import time
import os
class PlayVoice:
    def __init__(self, file_path="audio/"):
        current_script_path = os.path.abspath(__file__)
        parent_directory = os.path.dirname(current_script_path)
        parent_directory = os.path.dirname(parent_directory)
        audio_directory = os.path.join(parent_directory, file_path)
        if not os.path.exists(audio_directory):
            raise ValueError("The specified file path does not exist.")
        self.file_path = audio_directory
        self.allowed_colors = ["red", "yellow", "orange", "blue"]
        self.allowed_dashboards = ["1", "2", "3"]
        self.allowed_states = ["normal", "high", "low"]
    def play(self,color,dashboard,state):
        '''  check if the color, dashboard, and state are valid, then play the corresponding audio file.'''
        if color not in self.allowed_colors:
            raise ValueError(f"Invalid color: {color}. Allowed colors are: {self.allowed_colors}")
        if dashboard not in self.allowed_dashboards:
            raise ValueError(f"Invalid dashboard: {dashboard}. Allowed dashboards are: {self.allowed_dashboards}")
        if state not in self.allowed_states:
            raise ValueError(f"Invalid state: {state}. Allowed states are: {self.allowed_states}")
        color_audio = f"{color}_area.mp3" if color in self.allowed_colors else ""
        dashboard_audio = f"dashboard_{dashboard}.mp3" if dashboard in self.allowed_dashboards else ""
        state_audio = f"{state}.mp3" if state in self.allowed_states else ""
        audio_files = [color_audio, dashboard_audio, state_audio]
        for audio_file in audio_files:
            if audio_file:
                full_path = os.path.join(self.file_path, audio_file)
                if os.path.exists(full_path):
                    playsound(full_path)
                else:
                    print(f"Audio file {full_path} does not exist.")
        time.sleep(0.1)